package com.hnx.admin.aurora.web.exception;

import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/*
创建人： 倔强的头发
创建时间：2025/6/14
功能描述:

*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultJson handleMethodArgumentNotValidException(MethodArgumentNotValidException e,HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        log.error("请求地址'{}',系统校验异常'{}'", requestURI, message);
        return ResultJson.fail(501,message);
    }
    @ExceptionHandler(Exception.class)
    public ResultJson exception(Exception ex, HttpServletRequest request){
        String requestURI = request.getRequestURI();
        String defaultMessage = ex.getMessage();
        log.error("请求地址'{}',系统异常'{}'", requestURI, defaultMessage);
        return ResultJson.fail(502,defaultMessage);
    }
}
